<?php
/* Inserts the XML array $arrXML to
 * the database with connection $con
 */
function insertSolarXmlToDatabase($arrXml, $con) {
	if ($result = 
            $con->query("SELECT `updated` FROM `sun` WHERE `updated` = \"".
                mysqli_real_escape_string($con, $arrXml['solardata']['updated']). 
                "\" LIMIT 5")) 
    {
	printf("<br>Select returned %d rows.\n", $result->num_rows);
        
        if ($result->num_rows > 0) 
        {
            printf("<br>The date <b>".$arrXml['solardata']['updated']."</b> exists!!!");
            /* free result set */
            $result->close();
        }
        else 
        {
            $arrXml ['solardata']['sol'] = calcSol($arrXml ['solardata']['updated']);
            /* free result set */
            $result->close();
            //insert the data in SQL
            $insertion = 
                $con->query("INSERT INTO sun VALUES(\"".
                    mysqli_real_escape_string($con, $arrXml ['solardata']['source']) ."\", \"" 
                    . mysqli_real_escape_string($con, $arrXml ['solardata']['updated']) ."\"" . ", \""
                    . mysqli_real_escape_string($con, $arrXml ['solardata']['sol']) ."\"" . ", \""
                    . mysqli_real_escape_string($con, $arrXml ['solardata']['solarflux']). "\"". ", \"" 
                    . mysqli_real_escape_string($con, $arrXml ['solardata']['aindex']) ."\"" . ", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['kindex']) ."\", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['kindexnt'])."\"".", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['xray'])."\"" .", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['sunspots'])."\"".", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['heliumline'])."\"" .", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['protonflux'])."\"" .", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['electonflux']) ."\"".", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['aurora']) ."\"" . ", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['normalization']) ."\"" . ", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['latdegree'])."\"" . ", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['solarwind'])."\"" .", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['magneticfield']) ."\"".", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['geomagfield']) ."\", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['signalnoise']) ."\"".", \""
					. mysqli_real_escape_string($con, $arrXml ['solardata']['fof2'])."\""
					.");");
            if ($insertion === TRUE) 
			{
                printf("<br>Row added");
            }
            else
			{
                printf("<br>".$con->error);
            }
        }
    }
	else 
	{
		printf("<br>".$con->error);
	}
}

function calcSol($date)
{
    define("START_DATE_STAMP", 947116800);
    define("CURIOSITY_LAND_STAMP", 1344231120);
    $inputDateStamp = strtotime($date);
    $solOfCuriosityLand = (START_DATE_STAMP - CURIOSITY_LAND_STAMP)/88775.244 + 44795.9998;
    $solOfGivenDate = (START_DATE_STAMP - $inputDateStamp)/88775.244 + 44795.9998;
    $sol = floor($solOfCuriosityLand - $solOfGivenDate + 1);
    return $sol;
}

//// Under construction - modifing.



 //// Print the data from the database

function readSolarXmlFromDatabase($con, $updated="*") 
{
    //preventing SQL injection
    $date = mysqli_real_escape_string($con, $updated);
    if($date === "*") 
    {
        $extractDataQuery = "SELECT * FROM `sun`;";
    }
    else 
    {
        $extractDataQuery = "SELECT * FROM `sun` WHERE `updated` = \"".$updated."\";";
    }
    
    if ($result = $con->query($extractDataQuery)) 
    {
        while($row = mysqli_fetch_array($result))
        {
             echo "<br>Sol: ".$row['sol'] . " | Updated: " . $row['updated']. " | Solar flux: " . $row['solarflux']. " | A index: " . $row['aindex']. " | K index: " . $row['kindex'];
             echo "<br />";
         }
          
          //free extractDataQuery set
          $result->Close();
    }
    else
	{
        printf($con->error);
    }
}

?>